#include "Bullet.h"
#include "../../../SGD_Wrappers/CSGD_TextureManager.h"
#include "../../../SGD_Wrappers/CSGD_Direct3D.h"
#include "../../DestroyEntityMessage.h"
#include "../../../SGD_Wrappers/IMessage.h"
#include "../../../SGD_Wrappers/CSGD_MessageSystem.h"
#include "../../../SGD_Wrappers/CSGD_EventSystem.h"

CBullet::CBullet(void)
{
	m_fLifeTime = 2.0f;
	m_nDamage = 20;
}


CBullet::~CBullet(void)
{
	Owner->Release();
}

void CBullet::Render( void )
{
	//CSGD_TextureManager::GetInstance()->Draw(
	//	GetImageID(), GetPosX() - (GetWidth()/2),
	//	GetPosY() - (GetHeight()/2), 
	//	1.0f, 1.0f, nullptr,
	//	(GetWidth()/2), (GetHeight()/2),
	//	m_fcurrRot);

	CSGD_Direct3D::GetInstance()->DrawRect( GetRect(), 0, 0, 255 );

}

void CBullet::Update(float fElapsedTime)
{
	m_fLifeTime -= fElapsedTime;
	if( m_fLifeTime <= 0.0f )
	{
			IMessage* i = new CDestroyEntityMessage( this );
			CSGD_MessageSystem::GetInstance()->SendMsg( i );
			i = nullptr;
	}
	CEntity::Update( fElapsedTime );
}

bool CBullet::CheckCollision( IEntity* pOther)
{
	if( CEntity::CheckCollision( pOther ) )
	{
		if( Owner->GetType() == pOther->GetType() )
			return false;
		else
		{
			IMessage* i = new CDestroyEntityMessage( this );
			CSGD_MessageSystem::GetInstance()->SendMsg( i );
			i = nullptr;
			

			CSGD_EventSystem::GetInstance()->SendEvent("PEON_ATTACK",(void*)m_nDamage,pOther,this);
			
			
			return true;
		}
	}
	return false;
}


float	CBullet::GetRotation( void )
{
	return m_fcurrRot;
}

void	CBullet::SetRotation( float r )
{
	m_fcurrRot = r;
}

CEntity*	CBullet::CGetOwner( void ) const
{
	return Owner;
}

void	CBullet::SetOwner( CEntity* p )
{
	Owner = p;
	Owner->AddRef();
}


int		CBullet::GetDamage( void )
{
	return m_nDamage;
}

void	CBullet::SetDamage( int d )
{
	m_nDamage = d;
}
